package labox.innovation.gameserver.skills.l2skills;

import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import labox.innovation.config.Config;
import labox.innovation.gameserver.model.L2Effect;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.L2Object;
import labox.innovation.gameserver.model.L2Skill;
import labox.innovation.gameserver.model.actor.L2Character;
import labox.innovation.gameserver.model.actor.L2Npc;
import labox.innovation.gameserver.model.actor.L2Playable;
import labox.innovation.gameserver.model.actor.L2Summon;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.StatusUpdate;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.gameserver.skills.Formulas;
import labox.innovation.gameserver.templates.StatsSet;

/**
 * ʹĿ����һ��ʱ��������˺� �����˺���ת��Ϊ����Hp
 */
public class L2SkillDrain extends L2Skill {
	private static final Logger _logDamage = Logger.getLogger("damage");

	private float _absorbPart;
	private int _absorbAbs;

	public L2SkillDrain(StatsSet set) {
		super(set);

		// �����˺���
		_absorbPart = set.getFloat("absorbPart", 0.f);
		// �����˺��ľ��ֵ
		_absorbAbs = set.getInteger("absorbAbs", 0);
	}

	@Override
	public void useSkill(L2Character activeChar, L2Object[] targets) {
		if (activeChar.isDead())
			return;

		for (L2Character target : (L2Character[]) targets) {
			if (target.isDead())
				continue;

			if (activeChar != target && target.isInvul())
				continue; // No effect on invulnerable chars unless they cast it
							// themselves.

			int mcrit = Formulas.calcAttactHit(activeChar, target);
			int damage = 0;
			if (mcrit == 2) {
				damage = (int) Formulas.calcMagicDam(activeChar, target, this, true);
			} else if (mcrit == 1) {
				damage = (int) Formulas.calcMagicDam(activeChar, target, this, false);
			}

			int _drain = 0;
			int _hp = (int) target.getCurrentHp();

			if (damage > _hp)
				_drain = _hp;
			else
				_drain = damage;

			double hpAdd = _absorbAbs + _absorbPart * _drain;
			double hp = ((activeChar.getCurrentHp() + hpAdd) > activeChar.getMaxHp() ? activeChar.getMaxHp() : (activeChar.getCurrentHp() + hpAdd));

			// ���� ʩ���ߵĵ�ǰѪ��
			activeChar.setCurrentHp((int) hp);

			StatusUpdate suhp = new StatusUpdate(activeChar.getObjectId());
			suhp.addAttribute(StatusUpdate.HP, (int) hp);
			activeChar.sendPacket(suhp);

			// Check to see if we should damage the target
			if (damage > 0 && !target.isDead()) {
				// activeChar.sendDamageMessage(target, damage, mcrit, false,
				// false);

				if (Config.LOG_GAME_DAMAGE && activeChar instanceof L2Playable && damage > Config.LOG_GAME_DAMAGE_THRESHOLD) {
					LogRecord record = new LogRecord(Level.INFO, "");
					record.setParameters(new Object[] { activeChar, " did damage ", (int) damage, this, " to ", target });
					record.setLoggerName("mdam");
					_logDamage.log(record);
				}

				// �����и���
				if (hasEffects()) {
					// ignoring vengance-like reflections
					if ((Formulas.calcSkillReflect(target, this) & Formulas.SKILL_REFLECT_SUCCEED) > 0) {
						activeChar.stopSkillEffects(getId());
						getEffects(target, activeChar);
						SystemMessage sm = new SystemMessage(SystemMessageId.YOU_FEEL_S1_EFFECT);
						sm.addSkillName(this);
						activeChar.sendPacket(sm);
					} else {
						// activate attacked effects, if any
						target.stopSkillEffects(getId());
						if (Formulas.calcSkillSuccess(activeChar, target, this))
							getEffects(activeChar, target);
						else {
							SystemMessage sm = new SystemMessage(SystemMessageId.C1_RESISTED_YOUR_S2);
							sm.addCharName(target);
							sm.addSkillName(this);
							activeChar.sendPacket(sm);
						}
					}
				}

				target.reduceCurrentHp(damage, activeChar, this);
			}

		}

		// effect self :]
		L2Effect effect = activeChar.getFirstEffect(getId());
		if (effect != null && effect.isSelfEffect()) {
			// Replace old effect with new one.
			effect.exit();
		}
		// cast self effect if any
		getEffectsSelf(activeChar);
	}

}
